<?php

class Newsletters extends Controller {

	function Newsletters()
	{
		parent::Controller();	
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('newslettersmodel');  
        $this->load->library("form_validation");       
	}     
    
    function index()
    {


        $data["list"] = $this->newslettersmodel->admin_get_list();
        $data["response"] = get_response(); 
       
        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'newsletters'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/newsletters/newsletters_list', $data);   
        $this->load->view('admin/default/footer');    
        
    }
 
    function edit($id = NULL)
    {
        if($id == NULL) show_404();

        $data["item"] = $this->newslettersmodel->admin_get_item($id); 
        if(empty($data["item"])) show_404();

        $this->_process_form();

        $data["action"] = "update";
        $data["token"] = generate_token("newsletters",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'newsletters')); 
        $this->load->view('admin/newsletters/newsletters_form', $data);   
        $this->load->view('admin/default/footer');     

    }
       
    function add()
    {
        $this->_process_form();

        $data["item"] = array(
                "id_Newsletters" => "",
                "Date_Newsletters" => "", 
                "Summary_Newsletters" => "", 
                "URL_Newsletters" => "",
                "Image_Newsletters" => ""
        ); 

        $data["action"] = "create";
        $data["token"] = generate_token("newsletters",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));    
        $this->load->view("admin/default/nav_pages", array('selected' => 'newsletters'));
        $this->load->view('admin/newsletters/newsletters_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->newslettersmodel->admin_get_item($id, "id_Newsletters as id, Date_Newsletters as date, Summary_Newsletters as summary, URL_Newsletters as URL, Image_Newsletters as image, Approved_Newsletters as approved, Active_Newsletters as active ");

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/newsletters');

        } else {

            $data["controller"] = "newsletters";
            $this->load->model('glossarymodel');

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'newsletters'));
            $this->load->view('admin/newsletters/newsletters_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }  
    function preview($id = NULL)
    {

        if($this->input->post("submit_action") === FALSE){

            $data["item"] = $this->newslettersmodel->admin_get_item($id, "id_Newsletters as id, Date_Newsletters as date, Summary_Newsletters as summary, URL_Newsletters as URL, Image_Newsletters as image, Approved_Newsletters as approved, Active_Newsletters as active "); 
            if (empty($data["item"])) show_404();             

            $data["action"] = "view";

        } else {

            $data["item"] = array(
                'id' => $this->input->post("id", TRUE),
                'date' => $this->input->post("date", TRUE),  
                'summary' => $this->input->post("summary", TRUE),  
                'URL' => $this->input->post("URL", TRUE),
                'image' => $this->input->post("image", TRUE));

            $data["action"] = "preview";

        }

        $data["controller"] = "newsletters";
        $data["image_folder"] = base_url().UPLOADS_FOLDER.NEWS_IMG_FOLDER;		
        $this->load->model('glossarymodel');
        $this->load->view('admin/previews/preview_newsletter_template', $data);   

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->newslettersmodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/newsletters');

        } else {

            $this->newslettersmodel->admin_delete($id);
            set_response('success', "The item <i>".$data["item"]["Date_Newsletters"]."</i> was deleted.");
            redirect('admin/newsletters');
        }
    }
    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Newsletters' => 0);
        $this->newslettersmodel->admin_update($id, $data);
        $item = $this->newslettersmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Date_Newsletters"].'</i> is now inactive. <a href="'.site_url("admin/newsletters/preview/".$item["id_Newsletters"]).'" rel="view">Click to view</a>');
        redirect('admin/newsletters');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Newsletters' => 1);
        $this->newslettersmodel->admin_update($id, $data);
        $item = $this->newslettersmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Date_Newsletters"].'</i> is now active. <a href="'.site_url("admin/newsletters/preview/".$item["id_Newsletters"]).'" rel="view">Click to view</a>');
        redirect('admin/newsletters');
    }
    function approve($id = false){

        if(!is_allowed(array(ROLE_ADMIN,ROLE_PUBLISHER))) show_404();
        if($id == NULL) show_404();  

        $data = array('Approved_Newsletters' => 1, 'Active_Newsletters' => 1);
        $this->newslettersmodel->admin_update($id, $data);
        $item = $this->newslettersmodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Date_Newsletters"].'</i> was approved. <a href="'.site_url("admin/newsletters/preview/".$item["id_Newsletters"]).'" rel="view">Click to view</a>');
        redirect('admin/newsletters');
    }
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("newsletters",$this->input->post("token",TRUE))){

                    $data = array(
                        'Date_Newsletters' => $this->input->post("date"),
                        'Summary_Newsletters' => $this->input->post("summary"),  
                        'URL_Newsletters' => $this->input->post("url"),
                        'Image_Newsletters' => $this->input->post("image"),
                    );

                    switch($this->input->post("submit_action")){    

                        case 'create':

                            $new_id = $this->newslettersmodel->admin_insert($data);
                            set_response('success','item <i>'.$data['Date_Newsletters'].'</i> was created. <a href="'.site_url("admin/newsletters/preview/".$new_id).'" rel="view">Click to view</a>');

                        break;
                        case 'update':

                            $this->newslettersmodel->admin_update($this->input->post("id"), $data);
                            set_response('success','item <i>'.$data['Date_Newsletters'].'</i> was updated. <a href="'.site_url("admin/newsletters/preview/".$this->input->post("id")).'" rel="view">Click to view</a>');

                        break;        

                    }

                    redirect("admin/newsletters");

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config = array( 
                        array(
                         'field'   => 'date',
                         'label'   => 'Date',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'summary',
                         'label'   => 'Summary',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'url',
                         'label'   => 'URL',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'image',
                         'label'   => 'Image',
                         'rules'   => 'trim|xss_clean'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */